// misconf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "soapH.h"
#include "ServiceSoap.nsmap"

#define progname	"misconf"
#define	version		"1.0.1 x64"
#define DEBUG				1

void usage(void)
{
	fprintf(stderr, "Recieve MES config data from Shopfloor tool   Version: %s\n"
					"\n(c) Tody 2018, T-ware Inc.\n"
					"\nUsage:\t%s -url http://172.16.80.161:801/Service.asmx -sn <Serialnumber>"
					"\nOptions:\n"
					"      -url <website>       website must be assigned\n"					
					"      -sn  <Serialnumber>  get Config from MES via Serial number\n", version, progname );
	exit(1);
}

int main(int argc, char *argv[])
{
	struct soap 					*soap;
	struct _CFG__GetSoftInfo 		sn;
	struct _CFG__GetSoftInfoResponse 	retData;
	
	char MISDATA[10240];   /*Size is ok for later use?*/
	char *website=NULL;
	char *father=NULL;
	char realweb[512];
	int retCode = 0;
	
	memset(MISDATA, 0, sizeof(MISDATA));
	
	if (argc >= 3 && strcmp("-url" ,argv[1])!=0)
	{
		usage();
	}else if (argc >= 3 && strstr(argv[2], "http://") != NULL)
	{
		memset(realweb, 0, 512);
		strcpy(realweb, argv[2]);
		website = realweb;
	}else{
		/*using default website.*/
		website = NULL;
	}

	printf("Accessing %s...\n", website==NULL? "Default website":website);

	if ( argc == 5 && strcmp("-sn", argv[3])==0 )
	{
		soap = soap_new();

		sn.sn = argv[4];
		retData.GetSoftInfoResult = MISDATA;

		soap_call___CFG2__GetSoftInfo(soap, website, NULL, &sn, &retData);
		if (soap->error) {
			printf("[-] network issue!\n");
			soap_print_fault(soap, stderr);
			retCode = 1;
		}else{
			if (strlen(retData.GetSoftInfoResult) != 0)
			{
				fprintf(stdout, "\n-------------------------------------------------------------------------------"); 
				//fprintf(stdout, "\nMIS: %s", retData.GetSoftInfoResult); 
				father= strtok(retData.GetSoftInfoResult, ",");
				while (father!=NULL)
				{
					fprintf(stdout, "\n%s", father); 
					father = strtok(NULL, ",");
				}
				fprintf(stdout, "\n-------------------------------------------------------------------------------");
				fflush(stdout);
			}else{
				soap_end(soap);
				free(soap);
				printf(" -> data not found!");
				retCode = 1;
			}
		}
	}else{
		usage();
	}

	printf("\nReturn code: %d", retCode);

	/* destory soap connection */
	soap_destroy(soap);
	soap_end(soap);
	soap_done(soap);

	exit(retCode);  /*normal exit from program*/
}

